package ru.susu.algebra.centralunits.alternating.initializers;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import ru.susu.algebra.centralunits.alternating.MathMethodWithInitializers;
import ru.susu.algebra.chartable.constructor.AlternatingCharTableUtil;
import ru.susu.algebra.field.quadratic.QuadraticField;
import ru.susu.algebra.field.quadratic.QuadraticFieldGeneralForm;
import ru.susu.algebra.field.quadratic.QuadraticInteger;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.properties.IPropertySource;

/**
 * @author akargapolov
 * @since: 02.09.2010
 */
public class QuadraticFieldsInitializer extends MathMethodWithInitializers<Object>
{
	private static final Class[] DEPENDENCIES = {SpecialRowsInitializer.class};

	private static final String QUADRATIC_FIELD_2_ROW = "QuadraticField2Row";
	private static final String ROW_2_QUADRATIC_FIELD = "Row2QuadraticField";

	@Override
	protected Object directRun(IPropertySource ps) throws Exception
	{
		List<Partition> rows = SpecialRowsInitializer.listSpecialRows(ps);
		if (ps.getValue(QUADRATIC_FIELD_2_ROW) == null)
			ps.setValue(QUADRATIC_FIELD_2_ROW, AlternatingCharTableUtil.field2Row(rows));
		if (ps.getValue(ROW_2_QUADRATIC_FIELD) == null)
			ps.setValue(ROW_2_QUADRATIC_FIELD, AlternatingCharTableUtil.row2Field(rows));

		return null;
	}

	@Override
	protected Class[] getDependentInitializers()
	{
		return DEPENDENCIES;
	}

	public static Map<QuadraticField, Partition> getField2Row(IPropertySource ps)
	{
		return (Map<QuadraticField, Partition>)ps.getValue(QUADRATIC_FIELD_2_ROW);
	}

	public static Map<Partition, QuadraticField> getRow2Field(IPropertySource ps)
	{
		return (Map<Partition, QuadraticField>)ps.getValue(ROW_2_QUADRATIC_FIELD);
	}

	public static QuadraticInteger getCharacter(IPropertySource ps, Partition partition)
	{
		return getRow2Field(ps).get(partition).getGenerateInteger();
	}

	public static QuadraticFieldGeneralForm getGeneralUnit(IPropertySource ps, Partition partition)
	{
		return getRow2Field(ps).get(partition).getGeneralUnit();
	}

	public static QuadraticFieldGeneralForm getOmega(IPropertySource ps, Partition partition)
	{
		return new QuadraticFieldGeneralForm(BigInteger.ZERO, BigInteger.ONE, getCharacter(ps, partition).getD());
	}

	public static QuadraticFieldGeneralForm getOmega2(IPropertySource ps, Partition p) throws Exception
	{
		return (QuadraticFieldGeneralForm)getOmega(ps, p).pow(BigInteger.valueOf(2));
	}

}
